using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PerformanceTimers
{
    public class Button
    {
        public Action Clicked;
        public string Text { get; set; }
        public Rectangle Area { get; set; }

        private SpriteFont font;
        private bool mouseDown;
        private bool mouseOver;

        public Button(SpriteFont font)
        {
            this.font = font;
        }

        public bool Update()
        {
            bool clicked = false;
            var mouse = Mouse.GetState();
            mouseOver = Area.Contains(mouse.X, mouse.Y);
            if (mouseOver)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                    mouseDown = true;
                else
                {
                    if (mouseDown)
                    {
                        OnClicked();
                        clicked = true;
                    }

                    mouseDown = false;
                }
            }
            else
                mouseDown = false;

            return clicked;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(font, Text, new Vector2(Area.X, Area.Y), mouseOver && !mouseDown ? Color.Yellow : Color.White);
        }

        private void OnClicked()
        {
            if (Clicked != null)
                Clicked();
        }
    }
}
